package com.godyao.mall.pms.controller;

import java.util.Arrays;
import java.util.Map;

import com.godyao.mall.pms.vo.request.AttrRequestVo;
import com.godyao.mall.pms.vo.response.AttrResponseVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.godyao.mall.pms.entity.AttrEntity;
import com.godyao.mall.pms.service.AttrService;
import com.godyao.mall.core.utils.PageUtils;
import com.godyao.mall.core.model.ResultMsg;



/**
 * 商品属性
 *
 * @author godyao
 * @email godyao0801@qq.com
 * @date 2022-04-19 14:42:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("pms/attr")
public class AttrController {

    private final AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/{attrType}/list/{categoryId}")
    public ResultMsg list(@PathVariable String attrType, @PathVariable Long categoryId,@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryDetail(attrType, categoryId, params);

        return ResultMsg.resultSuccess(page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ResultMsg list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return ResultMsg.resultSuccess(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public ResultMsg info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
		AttrResponseVo attr = attrService.getDetailById(attrId);

        return ResultMsg.resultSuccess(attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResultMsg save(@RequestBody AttrRequestVo attr){
		attrService.saveDetail(attr);

        return ResultMsg.resultSuccess("OK");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResultMsg update(@RequestBody AttrRequestVo attr){
		attrService.updateAttr(attr);
		//attrService.updateById(attr);

        return ResultMsg.resultSuccess("OK");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResultMsg delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return ResultMsg.resultSuccess("OK");
    }

}
